/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：CouponDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.ICouponDao;
import com.xteam.ecsystem.entities.BaseCouponInfo;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 购物券数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class CouponDaoImpl extends DDLPrefix implements ICouponDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public String getCouponList(int pageIndex, int pageSize, String bciName,
			String bciState) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseCouponInfo.REF + SQL_WHERE +" 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + BaseCouponInfo.REF + SQL_WHERE +" 1=1 ";
			
			if (bciName != null && bciName.length() > 0
					&& !bciName.equals("undefined")
					&& !bciName.equals("null")) {
				hql += SQL_AND + " bciName " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " bciName " + SQL_LIKE + " ? ";
				list.add("%" + bciName + "%");

			}
			
			if(bciState != null && bciState.length() > 0
					&& !bciState.equals("undefined")
					&& !bciState.equals("null")){
				if(!"-1".equals(bciState)){
					hql += SQL_AND + " bciState=? ";
					countHQL += SQL_AND + " bciState=? ";
					list.add(Integer.parseInt(bciState));
				}
			}

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, String id) {
		try {
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public String getCouponInfoById(String id) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseCouponInfo.REF + SQL_WHERE +" 1=1 " + SQL_AND + " bciId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseCouponInfo)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}
	
	public BaseCouponInfo getCouponInfo(String id){
		try {
			String hql = SQL_FROM + BaseCouponInfo.REF + SQL_WHERE +" 1=1 " + SQL_AND + " bciId=?";
			Object obj = this.baseDao.findObject(hql, new Object[]{id});
			
			if (obj != null) {
				return (BaseCouponInfo)obj;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public void updateCouponStateById(String id) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseCouponInfo");
			
			BaseCouponInfo coupon = (BaseCouponInfo)this.baseDao.get(clazz, id);
			if(coupon.getBciState() == 1){
				coupon.setBciState(0);
			}else{
				coupon.setBciState(1);
			}
			
			this.baseDao.saveOrUpdate(coupon);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void delCouponByIds(String bciIds) {
		if(bciIds != null){
			String oId[] = bciIds.split(",");
			for (String id : oId) {
				String hql = "";
				try {
					hql = SQL_DELETE + SQL_FROM + BaseCouponInfo.REF + SQL_WHERE
							+ " bciId = ? ";
					this.baseDao.executeUpdate(hql, new Object[] { id });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
